package com.k.generator.commentgenerator;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;

import java.util.Properties;

/**
 * 自定义注释生成器
 * Usage
 * <commentGenerator type="com.k.generator.commentgenerator.MyCommentGenerator">
 * <property name="addRemarkComments" value="true"/>
 * </commentGenerator>
 */
public class MyCommentGenerator extends DefaultCommentGenerator {
    private boolean addRemarkComments = true;
    private static final String EXAMPLE_SUFFIX = "Example";
    private static final String MAPPER_SUFFIX = "Mapper";
    private static final String API_MODEL_PROPERTY_FULL_CLASS_NAME = "io.swagger.annotations.ApiModelProperty";
    private static final String JSON_FORMAT = "com.fasterxml.jackson.annotation.JsonFormat";
    public static final String API_MODEL_FULL_CLASS_NAME = "io.swagger.annotations.ApiModel";

    /**
     * 设置用户配置的参数
     */
    @Override
    public void addConfigurationProperties(Properties properties) {
        super.addConfigurationProperties(properties);
        this.addRemarkComments = StringUtility.isTrue(properties.getProperty("addRemarkComments"));
    }

    /**
     * Model 类注释
     *
     * @param modelClass        modelClass
     * @param introspectedTable table
     */
    @Override
    public void addModelClassComment(TopLevelClass modelClass, IntrospectedTable introspectedTable) {
        //super.addModelClassComment(modelClass, introspectedTable);
        String remarks = introspectedTable.getRemarks();
        System.out.println("addModelClassComment : " + remarks);

        String author = "ljw";
        modelClass.addJavaDocLine("/**");
        modelClass.addJavaDocLine(" * " + remarks);
        modelClass.addJavaDocLine(" *");
        modelClass.addJavaDocLine(" * @author " + author);
        modelClass.addJavaDocLine(" */");
        modelClass.addJavaDocLine("@ApiModel(value = \"" + remarks + "\")");
    }

    /**
     * 给字段添加注释
     */
    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable,
                                IntrospectedColumn introspectedColumn) {
        String remarks = introspectedColumn.getRemarks();
        //根据参数和备注信息判断是否添加swagger注解信息
        if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
//            addFieldJavaDoc(field, remarks);
            //数据库中特殊字符需要转义
            if (remarks.contains("\"")) {
                remarks = remarks.replace("\"", "'");
            }
            //A 给model的字段添加swagger注解
            field.addJavaDocLine("@ApiModelProperty(value = \"" + remarks + "\")");
            System.out.println("addFieldComment: fieldType:" + field.getType() + " fieldName:" + field.getName() + " remarks:" + remarks);
        }
        //B 给时间字段规定格式
        if (field.getType().equals(FullyQualifiedJavaType.getDateInstance())) {
            field.addJavaDocLine("@JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\",timezone = \"GMT+8\")");
        }
    }

    /**
     * 引用添加
     *
     * @param compilationUnit compilationUnit
     */
    @Override
    public void addJavaFileComment(CompilationUnit compilationUnit) {
        super.addJavaFileComment(compilationUnit);
        //只在model中添加swagger注解类的导入
        if (!compilationUnit.getType().getFullyQualifiedName().contains(MAPPER_SUFFIX) && !compilationUnit.getType().getFullyQualifiedName().contains(EXAMPLE_SUFFIX)) {
            compilationUnit.addImportedType(new FullyQualifiedJavaType(API_MODEL_PROPERTY_FULL_CLASS_NAME));
            compilationUnit.addImportedType(new FullyQualifiedJavaType(JSON_FORMAT));
            compilationUnit.addImportedType(new FullyQualifiedJavaType(API_MODEL_FULL_CLASS_NAME));
        }
    }

    /**
     * getter 方法注释
     *
     * @param method             method
     * @param introspectedTable  introspectedTable
     * @param introspectedColumn introspectedColumn
     */
    @Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        //super.addGetterComment(method, introspectedTable, introspectedColumn);
    }

    /**
     * setter 方法注释
     *
     * @param method             method
     * @param introspectedTable  introspectedTable
     * @param introspectedColumn introspectedColumn
     */
    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        //super.addSetterComment(method, introspectedTable, introspectedColumn);
    }
}
